﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinEntity;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using System.Data;
using EmoneyWeiXin.Filter;
namespace EmoneyWeiXin.Controllers
{
    public class KeyWordManagerController : Controller
    {
        //
        // GET: /KeyWordManager/
          [LoginAuthorizeAttribute]
        public ActionResult Index()
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            return View();
        }
        //查询关键字列表
        public string GetKeyWordList()
        {

            var total = 0;
            var pindex = int.Parse(Request["index"]);
            var psize = int.Parse(Request["size"]);
            var weixinid = int.Parse(Request["weixinid"]);
            var list = KeyWordManagerBLL.getKeyWordManagerList(pindex, psize, weixinid, out  total);
            return JsonTools.ToJson(new { total = total, dataList = list });


        }
        //根据id查询关键字
        public string GetKeyWordListByID()
        {
            //  var strJson = "";
            var id = int.Parse(Request["id"]);
            //DataTable table=null;
            var list = KeyWordManagerBLL.getKeyWordManagerListByID(id);
            //if (list != null)
            //   // strJson = JsonTools.ToJson(list);

            //查询图文列表
            // var strJson2 = "";
            var newsreportList = KeyWordManagerBLL.getKeyWordManager_NewsReportListByID(id);
            //if (newsreportList != null && newsreportList.Tables.Count > 0)
            //{
            DataTable table = newsreportList.Tables[0];
            //strJson2 = JsonTools.ToJson(table);
            //}
            return JsonTools.ToJson(new { list1 = list, list2 = table });

            //return strJson + "~" + strJson2;
        }

        [HttpPost]
        [ValidateInput(false)]
        //更新关键字方法
        public string UpdateKeyWord()
        {
            var str = "";
            var op = int.Parse(Request["op"]);
            var id = int.Parse(Request["id"]);
            var ire = 0;
            if (op != -1)//编辑操作
            {
                var weixinid = int.Parse(Request["weixinid"]);
                var keyword = Request["keyword"];
                var content = Request["content"];
                var validStartTime = (Request["validStartTime"] == null || Request["validStartTime"] == "") ? DateTime.Now : DateTime.Parse(Request["validStartTime"]);
                var validEndTime = (Request["validEndTime"] == null || Request["validEndTime"] == "") ? DateTime.Now : DateTime.Parse(Request["validEndTime"]);
                var isvalid = int.Parse(Request["isvalid"]);
                var islongvalid = int.Parse(Request["islongvalid"]);
                var newsreportids = Request["newsreportids"];
                var obj = new KeyWordManagerEntity();
                obj.id = id;
                obj.keyword = keyword;
                obj.replyContent = content;
                obj.validStartTime = validStartTime;
                obj.validEndTime = validEndTime;
                obj.isvalid = isvalid;
                obj.isLongValid = islongvalid;
                obj.isdel = 0;
                obj.createtime = DateTime.Now;
                obj.updatetime = DateTime.Now;
                obj.weixinid = weixinid;
                ire = KeyWordManagerBLL.UpdateKeyWordManager(obj, newsreportids.Trim('^'));

            }
            else//删除操作
            {
                ire = KeyWordManagerBLL.DeleteKeyWordManager(id);

            }
            if (ire > 0)
            {
                str = "1";
            }
            return str;
        }

    }
}
